package ecut.set;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


public class EcutSet<E> implements Set<E> {
	private int count;
	private Map<Object, Object> map= new HashMap<>();
	private Object DEFAULT_VALUE=null;

	@Override
	public boolean containsAll(Collection<?> c) {
		Object[] array=c.toArray();
		for(int i=0;i<array.length;i++) {
			if(!this.contains(array[i])) {
				return false;
			}
		}
		return true;
		// TODO Auto-generated method stub
	}
	
	@Override
	public boolean add(E e) {
		if((this.contains(null) && e==null) || this.contains(e)) {
			return false;
		}
		else {
			map.put(e, this.DEFAULT_VALUE);
			count++;
			return true;
		}
		// TODO Auto-generated method stub
	}
	
	@Override
	public int size() {
		// TODO Auto-generated method stub
		return count;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return count==0?true:false;
	}

	@Override
	public boolean contains(Object o) {
		if(map.containsKey(o)) {
			return true;
		}
		else return false;
		// TODO Auto-generated method stub
	}

	@Override
	public Iterator<E> iterator() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object[] toArray() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> T[] toArray(T[] a) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public boolean remove(Object o) {
		if(this.contains(o)) {
			map.remove(o);
			count--;
			return true;
		}
		return false;
		// TODO Auto-generated method stub
	}

	public String toString() {
		return map.toString();
	}

	@Override
	public boolean addAll(Collection<? extends E> c) {
		@SuppressWarnings("unchecked")
		int size=count;
		E[] array=(E[]) c.toArray();
		for(int i=0;i<array.length;i++) { this.add(array[i]);  }
		// TODO Auto-generated method stub
		return count>size?true:false;
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		// TODO Auto-generated method stub
		boolean flag=false;
		Map map2=new HashMap<Object, Object>();
		Object[] array=c.toArray();
		for(int i=0;i<array.length;i++) {
			if(this.contains(array[i])) {
				map2.put(array[i], this.DEFAULT_VALUE);
				flag=true;
			}
		}
		map=map2;
		return flag;
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		// TODO Auto-generated method stub
		int size=count;
		@SuppressWarnings("unchecked")
		E[] array=(E[]) c.toArray();
		for(int i=0;i<array.length;i++) { this.remove(array[i]);  }
		return count<size?true:false;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		map.clear();
		count=0;
	}

}
